<template>
  <!-- 条件栏 -->
  <page-filter
    v-model:query="filterInfo.query"
    :filter-list="filterInfo.list"
  />
  <dialog-table
    ref="resourceTable"
    className="reprotTable"
    :refresh="tableInfo.refresh"
    :init-curpage="tableInfo.initCurpage"
    v-model:data="tableInfo.data"
    :api="tableInfo.api"
    row-key="id"
    :query="filterInfo.query"
    :field-list="tableInfo.fieldList"
    :handle="tableInfo.handle"
    :highlight-row="highlightRow"
    :check-box="checkBox"
    :reserve-selection="true"
    :checked-row-list="checkedRowList"
    :checkedList="checkBox ? checkedList : []"
    @handleEvent="handleEvent"
  />
</template>

<script setup name="selectDeptReport">
import { getDeptPage } from '@/api/core.js'
const { proxy } = getCurrentInstance()
const props = defineProps({
  highlightRow: {
    type: Boolean,
    default: false,
  },
  checkBox: {
    type: Boolean,
    default: true,
  },
  api: {
    type: Function,
    default: getDeptPage,
  },
  query: {
    type: Object,
    default: function () {
      return {}
    },
  },
  checkedRowList: {
    type: Array,
    default: function () {
      return []
    },
  },
  saveEmpty: {
    type: Boolean,
    default: false,
  },
  checkedList: {
    type: Array,
    default: function () {
      return []
    },
  },
  fieldList: {
    type: Array,
    default: function () {
      return [
        { label: '名称', value: 'name' },
        { label: '编码', value: 'code' },
      ]
    },
  },
  filterInfoList: {
    type: Array,
    default: function () {
      return []
    },
  },
})

// 过滤相关配置
const filterInfo = ref({
  query: {
    sort: 'create_time desc',
    status: 1,
    ...props.query,
  },
  list: [
    {
      type: 'input',
      label: '名称',
      value: 'nameLike',
      event: search,
    },
    { type: 'button', label: '搜索', event: search },
  ],
})
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  api: undefined,
  data: [],
  fieldList: props.fieldList,
})
const selectRow = ref('')

onMounted(() => {
  // hack, 在table创建出来之后再去调用
  proxy.$nextTick(() => {
    getList()
    fullQuery()
  })
})
function fullQuery() {
  if (props.filterInfoList.length) {
    let arr = [{ type: 'button', label: '搜索' }]
    filterInfo.value.list = props.filterInfoList.concat(arr)
    filterInfo.value.list.forEach((item) => {
      item.event = search
    })
  }
}

// 获取列表
function getList() {
  tableInfo.value.api = props.api
  tableInfo.value.refresh = Math.random()
}
function search() {
  tableInfo.value.api = props.api
  tableInfo.value.initCurpage = Math.random()
  tableInfo.value.refresh = Math.random()
}
window.addEventListener('message', receiveMessageIframePage, false)
function receiveMessageIframePage(data) {
  let res = data.data
  // 重置选中
  if (res.type === 'customDialogReset') {
    tableInfo.value.initCurpage = Math.random()
    proxy.$refs.resourceTable.clearSelection()
  }
}
function handleEvent(event, data) {
  switch (event) {
    case 'tableClickRow':
      handelClickRow(data)
      selectRow.value = data
      break
    case 'tableCheck':
      handelClickRow(data)
      selectRow.value = data
      break
  }
}
function handelClickRow(data) {
  window.parent.postMessage(
    { type: 'tableClickRow', data: handleCheckData(data) },
    '*'
  )
}
function handleCheckData(data) {
  return data.map((element) => {
    return {
      label: element.name,
      value: element.id,
    }
  })
}
</script>
